# gc();rm(list = ls());
gc()

# library(dplyr)
# library(ggplot2)
# library(tidyr)


load("results/mc/full.Rda")

dir.create("plots/mc/", recursive = TRUE)

both <- both %>% 
  mutate(cor_c = case_when(cor_t > quantile(cor_t, .8) ~ "high +",
                           cor_t < quantile(cor_t, .2) ~ "high -",
                           cor_t <= quantile(cor_t, .8) & cor_t > quantile(cor_t, .6) ~ "medium +",
                           cor_t >= quantile(cor_t, .2) & cor_t < quantile(cor_t, .4) ~ "medium -",
                           TRUE ~ "low"))
both$cor_c <- factor(both$cor_c, levels = c("high -", "medium -", "low", "medium +", "high +"))

# Salience ####
supp.labs <- c("Primary Dimension", "Secondary Dimension")
names(supp.labs) <- c("sal1", "sal2")

tmp <- both %>% 
  select(run:sal2, model, cor_c) %>% 
  pivot_longer(sal1:sal2) %>% 
  mutate(diff = ifelse(name == "sal1", value-(1-sal_t), value-sal_t))

tmp %>% 
  ggplot(aes(x = nsep_t, y = diff)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) +
  facet_wrap(~name, ncol = 2, labeller = labeller(name = supp.labs)) +
  theme_bw() + xlab("Non-separability Parameter") + ylab("Estimate - True Value") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/sals_e_cmbn.pdf", width = 6, height = 3.5)

tmp %>% 
  mutate(sal_t = paste("Sal. of Prim. Dim. =", 1-sal_t)) %>% 
  ggplot(aes(x = nsep_t, y = diff)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) +
  facet_grid(name~sal_t, labeller = labeller(name = supp.labs)) +
  theme_bw() + xlab("Non-separability Parameter") + ylab("Estimate - True Value") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/sals_e_sal.pdf", width = 10, height = 4)

tmp %>% 
  mutate(sal_t = paste("Sal. of Prim. Dim. =", 1-sal_t)) %>% 
  ggplot(aes(x = nsep_t, y = diff)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) +
  facet_grid(name~cor_c, labeller = labeller(name = supp.labs)) +
  theme_bw() + xlab("Non-separability Parameter") + ylab("Estimate - True Value") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/sals_e_cor.pdf", width = 10, height = 4)

# Correlation w true values ####
tmp <- both %>% 
  select(run:nsep_t, cor1, cor2, model, cor_c) %>% 
  pivot_longer(cor1:cor2) %>% 
  mutate(name = ifelse(name == "cor1", "Primary Dimension", "Secondary Dimension")) 

tmp %>% 
  ggplot(aes(x = nsep_t, y = value)) + 
  geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_wrap(~name, ncol = 5) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Mean Correlation w/True Values") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/cor_cmbn.pdf", width = 6, height = 3.5)

tmp %>% 
  mutate(sal_t = paste("Sal. of Prim. Dim =", 1-sal_t)) %>% 
  ggplot(aes(x = nsep_t, y = value)) + 
  geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_grid(name~sal_t) +
  theme_bw() + xlab("Non-separability Parameter") + ylab("Mean Correlation w/True Values") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable")) 
ggsave(filename = "plots/mc/cor_cmbn_sal.pdf", width = 10, height = 4)

tmp %>% 
  ggplot(aes(x = nsep_t, y = value)) + 
  geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_grid(name~cor_c) +
  theme_bw() + xlab("Non-separability Parameter") + ylab("Mean Correlation w/True Values") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/cor_cmbn_cor.pdf", width = 10, height = 4)

# Correlation across dimensions ####
ggplot(both,
       aes(x = nsep_t, y = cordim)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Correlation across Dimensions") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/cor_dim_cmbn.pdf", width = 3.5, height = 3.5)

ggplot(both %>% mutate(sal_t = paste("Sal. of Prim. Dim. =", 1-sal_t)),
       aes(x = nsep_t, y = cordim)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_wrap(~sal_t, ncol = 5) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Correlation across Dimensions") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/cor_dim_sal.pdf", width = 10, height = 4)

ggplot(both %>% mutate(sal_t = paste("Sal. of Prim. Dim. =", 1-sal_t)),
       aes(x = nsep_t, y = cordim)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_wrap(~cor_c, ncol = 5) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Correlation across Dimensions") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable")) 
ggsave(filename = "plots/mc/cor_dim_cor.pdf", width = 10, height = 4)

# Nonseparability ####
tmp <- both %>% filter(model == "nsep") %>% mutate(sal_t = paste("Sal. of Prim. Dim. =", 1-sal_t))
tmp %>% 
  ggplot(aes(x = nsep_t, y = nsep - nsep_t)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  theme_bw() + coord_cartesian(ylim = c(-.1, .1)) +
  xlab("Non-separability Parameter") + ylab("Estimate - True Value") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + scale_linetype_discrete(labels = c("Non-separable"))
ggsave(filename = "plots/mc/nsep_e.pdf", width = 3.5, height = 3.5)

tmp %>% 
  ggplot(aes(x = nsep_t, y = nsep - nsep_t)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  theme_bw() + coord_cartesian(ylim = c(-.1, .1)) +
  facet_wrap(~sal_t, ncol = 4) +
  xlab("Non-separability Parameter") + ylab("Estimate - True Value") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + scale_linetype_discrete(labels = c("Non-separable"))
ggsave(filename = "plots/mc/nsep_e_sal.pdf", width = 10, height = 4)

tmp %>% 
  ggplot(aes(x = nsep_t, y = nsep - nsep_t)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  theme_bw() + coord_cartesian(ylim = c(-.1, .1)) +
  facet_wrap(~cor_c, ncol = 5) +
  xlab("Non-separability Parameter") + ylab("Estimate - True Value") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + scale_linetype_discrete(labels = c("Non-separable"))
ggsave(filename = "plots/mc/nsep_e_cor.pdf", width = 10, height = 4)

# RSquared ####
both %>% 
  select(run:nsep_t, rsq1, rsq2, model) %>% 
  pivot_longer(rsq1:rsq2) %>% 
  mutate(name = ifelse(name == "rsq1", "Primary Dimension", "Secondary Dimension"),
         sal_t = paste("Sal. of Prim. Dim =", 1-sal_t)) %>% 
  ggplot(aes(x = nsep_t, y = value)) + 
  geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_wrap(~name, ncol = 5) + 
  theme_bw() + xlab("Non-separability") + ylab("Mean R^2") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/rsq_e.pdf", width = 6, height = 3.5)

both %>% 
  select(run:nsep_t, rsq1, rsq2, model) %>% 
  pivot_longer(rsq1:rsq2) %>% 
  mutate(name = ifelse(name == "rsq1", "Primary Dimension", "Secondary Dimension"),
         sal_t = paste("Sal. of Prim. Dim. =", 1-sal_t)) %>% 
  ggplot(aes(x = nsep_t, y = value)) + 
  geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_grid(name~sal_t) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Mean R^2") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/rsq_e_sal.pdf", width = 10, height = 4)

both %>% 
  select(run:nsep_t, cor_c, rsq1, rsq2, model) %>% 
  pivot_longer(rsq1:rsq2) %>% 
  mutate(name = ifelse(name == "rsq1", "Primary Dimension", "Secondary Dimension"),
         sal_t = paste("Sal. of Prim. Dim. =", 1-sal_t)) %>% 
  ggplot(aes(x = nsep_t, y = value)) + 
  geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_grid(name~cor_c) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Mean R^2") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/rsq_e_cor.pdf", width = 10, height = 4)

# Gamma ####
both %>% 
  select(run:nsep_t, gamma1:gamma2, model, sal_t) %>% 
  pivot_longer(gamma1:gamma2) %>% 
  mutate(dimension = stringr::str_sub(name, -1, -1),
         dimension = ifelse(dimension == 1, "Primary Dimension", "Secondary Dimension")) %>% 
  mutate(name = ifelse(name == "gamma1", "Coef.", "Coef. 2")) %>% 
  ggplot(aes(x = nsep_t, y = value)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_wrap(~dimension, ncol = 2) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Mean Coefficient") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/gamma_e.pdf", width = 6, height = 3.5)

both %>% 
  select(run:nsep_t, gamma1:gamma2, model, sal_t) %>% 
  pivot_longer(gamma1:gamma2) %>% 
  mutate(dimension = stringr::str_sub(name, -1, -1),
         dimension = ifelse(dimension == 1, "Primary Dimension", "Secondary Dimension")) %>% 
  mutate(name = ifelse(name == "gamma1", "Coef.", "Coef. 2"),
         sal_t = paste("Sal. of Prim. Dim. =", 1-sal_t)) %>% 
  ggplot(aes(x = nsep_t, y = value)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_grid(dimension~sal_t) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Mean Coefficient") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/gamma_e_sal.pdf", width = 10, height = 4)

both %>% 
  select(run:nsep_t, gamma1:gamma2, model, sal_t, cor_c) %>% 
  pivot_longer(gamma1:gamma2) %>% 
  mutate(dimension = stringr::str_sub(name, -1, -1),
         dimension = ifelse(dimension == 1, "Primary Dimension", "Secondary Dimension")) %>% 
  mutate(name = ifelse(name == "gamma1", "Coef.", "Coef. 2"),
         sal_t = paste("Sal. of Prim. Dim. =", 1-sal_t)) %>% 
  ggplot(aes(x = nsep_t, y = value)) + geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  facet_grid(dimension~cor_c) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Mean Coefficient") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/gamma_e_cor.pdf", width = 10, height = 4)

# LOO ###
both %>% 
  select(run:nsep_t, loo, model) %>% 
  ggplot(aes(x = nsep_t, y = loo)) + 
  geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Mean Difference") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/loo_cmbn.pdf", width = 3.5, height = 3.5)

both %>% 
  select(run:nsep_t, loo, loo_se, model, sal_t) %>% 
  mutate(sal_t = paste("Sal. of Prim. Dim =", 1-sal_t),
         diff = ifelse(loo >= 0, 0, 1)) %>% 
  ggplot(aes(x = nsep_t, y = diff)) + 
  geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Share of Worse Performance") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/loo_cmbn_1.pdf", width = 3.5, height = 3.5)

both %>% 
  select(run:nsep_t, loo, loo_se, model, sal_t) %>% 
  mutate(sal_t = paste("Sal. of Prim. Dim =", 1-sal_t),
         diff = loo + 2 * loo_se,
         diff = ifelse(diff >= 0, 0, 1)) %>% 
  ggplot(aes(x = nsep_t, y = diff)) + 
  geom_smooth(aes(linetype = model), color = "black", size = .5, alpha = .2) + 
  theme_bw() + xlab("Non-separability Parameter") + ylab("Share of Significantly Worse Performance") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  scale_linetype_discrete(labels = c("Non-separable", "Separable"))
ggsave(filename = "plots/mc/loo_cmbn_sh.pdf", width = 3.5, height = 3.5)

# predictive accuracy ####
both %>% 
  select(run:nsep_t, pred, model) %>% 
  pivot_wider(values_from = pred, names_from = model) %>% 
  ggplot(aes(x = nsep, y = sep)) + 
  geom_point(alpha = .25) + 
  geom_smooth(color = "black", size = .5, alpha = .2) + 
  theme_bw() + xlab("Non-separable Specification") + ylab("Separable Specification") + 
  theme(legend.position = "bottom", legend.title = element_blank()) 
ggsave(filename = "plots/mc/pred.pdf", width = 3.5, height = 3.5)

# item complexity ####
both %>% 
  select(run:nsep_t, compl, model) %>% 
  pivot_wider(values_from = compl, names_from = model) %>% 
  ggplot(aes(x = nsep, y = sep)) + 
  geom_point(alpha = .25) + 
  theme_bw() + xlab("Non-separable Specification") + ylab("Separable Specification") + 
  theme(legend.position = "bottom", legend.title = element_blank()) + 
  geom_abline(aes(slope = 1, intercept = 0))
ggsave(filename = "plots/mc/compl.pdf", width = 3.5, height = 3.5)
